home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / pextnd.zip / PEXTEND.PAS next >
Pascal/Delphi Source File  |  1992-12-21  |  3KB  |  91 lines

  1. {$R-,S-}
  2.  
  3. {PEXTEND
  4.  ------------------------------------------------------------------
  5.  This unit provides a single function, DpmiExtendHandles, for
  6.  extending the file handle table for DOS protected mode applications
  7.  under Borland Pascal 7.0.
  8.  
  9.  The standard DOS call for this purpose (AH = $67) does odd things to
  10.  DOS memory when run from a BP7 pmode program. If you Exec from a
  11.  program that has extended the handle table, DOS memory will be
  12.  fragmented, leaving a stranded block of almost 64K at the top of DOS
  13.  memory. The function implemented here avoids this problem.
  14.  
  15.  If you haven't used an ExtendHandles function before, note that you
  16.  cannot get more handles than the FILES= statement in CONFIG.SYS
  17.  allows. (Other utilities such as FILES.COM provided with QEMM do the
  18.  same thing.) However, even if you have FILES=255, any single program
  19.  cannot open more than 20 files (and DOS uses up 5 of those) unless
  20.  you use a routine like DpmiExtendHandles. This routine allows up to
  21.  255 open files as long as the FILES= statement provides for them.
  22.  
  23.  This code works only for DOS 3.0 or later. Since (to my knowledge)
  24.  DPMI cannot be used with earlier versions of DOS, the code doesn't
  25.  check the DOS version.
  26.  
  27.  Don't call this function more than once in the same program.
  28.  
  29.  Version 1.0,
  30.    Written 12/15/92, Kim Kokkonen, TurboPower Software
  31. }
  32.  
  33. {$IFNDEF DPMI}
  34.   !! Error: this unit for DPMI applications only
  35. {$ENDIF}
  36.  
  37. unit PExtend;
  38.   {-Extend handle table for DOS protected mode applications}
  39.  
  40. interface
  41.  
  42. function DpmiExtendHandles(Handles : Byte) : Word;
  43.   {-Extend handle table to Handles size.
  44.     Returns 0 for success, else a DOS error code.
  45.     Does nothing and returns 0 if Handles <= 20.}
  46.  
  47. implementation
  48.  
  49. uses
  50.   WinApi;
  51.  
  52. function DpmiExtendHandles(Handles : Byte) : Word;
  53. type
  54.   DosMemRec =
  55.     record
  56.       Sele, Segm : Word;
  57.     end;
  58. var
  59.   OldTable : Pointer;
  60.   OldSize : Word;
  61.   NewTable : Pointer;
  62.   DosMem : DosMemRec;
  63. begin
  64.   DpmiExtendHandles := 0;
  65.   if Handles <= 20 then
  66.     Exit;
  67.  
  68.   {Allocate new table area in DOS memory}
  69.   LongInt(DosMem) := GlobalDosAlloc(Handles);
  70.   if LongInt(DosMem) = 0 then begin
  71.     DpmiExtendHandles := 8;
  72.     Exit;
  73.   end;
  74.  
  75.   {Initialize new table with closed handles}
  76.   NewTable := Ptr(DosMem.Sele, 0);
  77.   FillChar(NewTable^, Handles, $FF);
  78.  
  79.   {Copy old table to new. Assume old table in PrefixSeg}
  80.   OldTable := Ptr(PrefixSeg, MemW[PrefixSeg:$34]);
  81.   OldSize := Mem[PrefixSeg:$32];
  82.   move(OldTable^, NewTable^, OldSize);
  83.  
  84.   {Set new handle table size and pointer}
  85.   Mem[PrefixSeg:$32] := Handles;
  86.   MemW[PrefixSeg:$34] := 0;
  87.   MemW[PrefixSeg:$36] := DosMem.Segm;
  88. end;
  89.  
  90. end.
  91.